/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package loginRegister;

import java.io.IOException;
import java.io.PrintWriter;
import javafx.scene.control.TitledPane;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import java.sql.*;

/**
 *
 * @author DELL
 */
public class RegisterServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void wrong1() {
        String msg = "不允许有空，注册失败！！";
        String title = "信息提示";
        int type = JOptionPane.YES_NO_CANCEL_OPTION;
        JOptionPane.showMessageDialog(null, msg, title, type);

    }

    public void wrong2() {
        String msg = "两次输入密码不一致，请重新输入！";
        String title = "信息提示";
        int type = JOptionPane.YES_NO_CANCEL_OPTION;
        JOptionPane.showMessageDialog(null, msg, title, type);

    }

    public void wrong3() {
        String msg = "该用户名已注册！";
        String title = "信息提示";
        int type = JOptionPane.YES_NO_CANCEL_OPTION;
        JOptionPane.showMessageDialog(null, msg, title, type);

    }

    public void right() {
        String msg = "注册信息合格，注册成功！";
        String title = "信息提示";
        int type = JOptionPane.YES_NO_CANCEL_OPTION;
        JOptionPane.showMessageDialog(null, msg, title, type);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userName = new String(request.getParameter("userName").getBytes("ISO-8859-1"), "UTF-8");

        String password1 = new String(request.getParameter("password1").getBytes("ISO-8859-1"), "UTF-8");
        String password2 = new String(request.getParameter("password2").getBytes("ISO-8859-1"), "UTF-8");
        String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
        String sex = new String(request.getParameter("sex").getBytes("ISO-8859-1"), "UTF-8");

        String birth = request.getParameter("year") + "-" + request.getParameter("month") + "-" + request.getParameter("day");

        String nation = new String(request.getParameter("nation").getBytes("ISO-8859-1"), "UTF-8");
        String edu = new String(request.getParameter("edu").getBytes("ISO-8859-1"), "UTF-8");
        String phone = new String(request.getParameter("phone").getBytes("ISO-8859-1"), "UTF-8");

        String addr = new String(request.getParameter("addr").getBytes("ISO-8859-1"), "UTF-8");

        String email = new String(request.getParameter("email").getBytes("ISO-8859-1"), "UTF-8");

        if (userName.length() == 0 || password1.length() == 0 || password2.length() == 0 || name.length() == 0 || phone.length() == 0 || email.length() == 0) {
            wrong1();

            response.sendRedirect("http://localhost:8081/PersonInforMSys/register/register.jsp");
        } else if (!(password1.equals(password2))) {

            wrong2();

            response.sendRedirect("http://localhost:8081/PersonInforMSys/register/register.jsp");

        } else {

            try {

                Connection con = null;
                java.sql.Statement stmt = null;
                ResultSet rs = null;

                Class.forName("com.mysql.jdbc.Driver");

                String url = "jdbc:mysql://localhost:3306/person?useUnicode=true&characterEncoding=gbk";

                con = DriverManager.getConnection(url, "skyline", "739349");

                stmt = con.createStatement();
                String sql = "select * from user where userName='" + userName + "'";

                rs = stmt.executeQuery(sql);
                rs.last();
                int k = rs.getRow();
                if (k > 0) {
                    wrong3();

                    response.sendRedirect("http://localhost:8081/PersonInforMSys/register/register.jsp");

                } else {

                    String sql2 = "insert into  user(userName,password,name,sex,birth,nation,edu,phone,addr,email) values('" + userName + "','" + password1 + "','" + name + "','" + sex + "','" + birth + "','" + nation + "','" + edu + "','" + phone + "','" + addr + "','" + email + "')";

                    try {
                        stmt.executeUpdate(sql2);
                    } catch (Exception e) {
                        log("Error:", e);
                    }

                }

                rs.close();;
                stmt.close();
                con.close();
                right();
                response.sendRedirect("http://localhost:8081/PersonInforMSys/login.jsp");

            } catch (Exception e) {
            }

        }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
